<?php

class CFactory extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;
	
	public function _setup(){
		
		$this->_name=R_PRE."factories";
		
		$this->_primary="factoryid";
		
		parent::_setup();
		
	}
	
	public function createFactory($data){
		
		$factoryid=$this->insert($data);
		
		return $factoryid;
		
	}
	
	public function editFactory($factoryid,$data){
		
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('factoryid = ?', $factoryid);
		
		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
		
		
	}
	
	public function getDetail($factoryid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();

		//$locationTable=R_PRE."locationfls";
		
		$select->from($this->_name,'*');
		
		//$select->join($locationTable,"$this->_name.location_id=$locationTable.flid",'*');
		
		$select->where($this->_name.'.factoryid = ? ',$factoryid);
		
		return $db->fetchAll($select);
		
	}
	
	public function delFactorys($factoryidArray){
		
		$db=$this->getAdapter();
		
		$factoryidString=join(',',$factoryidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE factoryid IN ($factoryidString)");
		
		return $result;
		
	}
	
	
	
	public function getAllFactoryCounts($uid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,array('COUNT(*)'));
		
		$select->where(" uid = ? ",$uid);
		
		return $db->fetchOne($select);
		
	}
	
	public function getAllFactories($from,$counts,$uid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		$select->where(" uid = ? ",$uid);
		
		$select->order("factoryid DESC");
		
		if($counts!=0){
			
			$select->limit($counts, $from);
			
		}
		
		return $db->fetchAll($select);
		
	}

	
}